﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EnterpriseFramework.DataAccess.Validation
{
    public class NotValidRuleList : IEnumerable<Rule>
    {
        /// <summary>
        /// A privately wrapped dictionary of rule objects.
        /// </summary>
        private Dictionary<string, Rule> _NotValidRuleList = new Dictionary<string, Rule>();

        /// <summary>
        /// Adds a validation rule to the broken rules list if it is broken.
        /// </summary>
        internal void AddRule(Rule rule)
        {
            if (rule.IsNotValid)
                _NotValidRuleList[rule.Name] = rule;    // Adds or replaces the rule
            else
                if (_NotValidRuleList.ContainsKey(rule.Name))
                    _NotValidRuleList.Remove(rule.Name);    // Removes the rule
        }

        public int Count
        {
            get { return _NotValidRuleList.Count; }
        }

        /// <summary>
        /// Returns a rule by name.
        /// </summary>
        /// <remarks>
        /// Returns null if no rule is found.
        /// </remarks>
        public Rule GetRuleByName(string name)
        {
            if (_NotValidRuleList.ContainsKey(name))
                return _NotValidRuleList[name];
            else
                return null;
        }

        public IEnumerator<Rule> GetEnumerator()
        {
            return _NotValidRuleList.Values.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return ((IEnumerable<Rule>)this).GetEnumerator();
        }
    }
}
